<?php

use yii\db\Schema;
use yii\db\Migration;

class m160120_075354_update_condition_type_ids extends Migration
{
    public function up()
    {
        $sql = <<<FINE
insert into {{%condition_type}} (id, name) values
(14, '移动广告位类型定向'),
(15, '移动设备品牌定向'),
(16, '移动运营商定向'),
(17, '移动网络类型定向'),
(18, '移动操作系统定向'),
(19, '移动操作系统版本定向'),
(20, '时段定向');
FINE;
        $this->execute($sql);
        $this->addColumn('{{%condition_type}}', 'is_for_display', 'int(1) not null default 0 comment "是否为 display 定向内容" after name');
        $this->addColumn('{{%condition_type}}', 'is_for_video', 'int(1) not null default 0 comment "是否为 video 定向内容" after is_for_display');
        $this->addColumn('{{%condition_type}}', 'is_for_mobile', 'int(1) not null default 0 comment "是否为 mobile 定向内容" after is_for_video');
        $sql = <<<FINE
update {{%condition_type}} set is_for_display=1, is_for_video=1 where id < 14;
update {{%condition_type}} set is_for_mobile=1 where id in (2,3,7,8,10,12,14,15,16,17,18,19,20);
FINE;
        $this->execute($sql);
        $this->addColumn('{{%condition_type}}', 'table_name', 'varchar(64) not null comment "对应数据库表名" after name');
        $sql = <<<FINE
update {{%condition_type}} set table_name = 'condition_adspace_category' where id = 1;
update {{%condition_type}} set table_name = 'condition_ip' where id = 2;
update {{%condition_type}} set table_name = 'condition_zone' where id = 3;
update {{%condition_type}} set table_name = 'condition_instrument' where id = 4;
update {{%condition_type}} set table_name = 'condition_os' where id = 5;
update {{%condition_type}} set table_name = 'condition_browser' where id = 6;
update {{%condition_type}} set table_name = 'condition_language' where id = 7;
update {{%condition_type}} set table_name = 'condition_adspace' where id = 8;
update {{%condition_type}} set table_name = 'condition_brand' where id = 9;
update {{%condition_type}} set table_name = 'condition_frequency' where id = 10;
update {{%condition_type}} set table_name = 'condition_retarget' where id = 11;
update {{%condition_type}} set table_name = 'condition_weather' where id = 12;
update {{%condition_type}} set table_name = 'condition_audience' where id = 13;
update {{%condition_type}} set table_name = 'condition_mobile_adspace_type' where id = 14;
update {{%condition_type}} set table_name = 'condition_mobile_brand' where id = 15;
update {{%condition_type}} set table_name = 'condition_mobile_carrier' where id = 16;
update {{%condition_type}} set table_name = 'condition_mobile_network' where id = 17;
update {{%condition_type}} set table_name = 'condition_mobile_os' where id = 18;
update {{%condition_type}} set table_name = 'condition_mobile_os_version' where id = 19;
update {{%condition_type}} set table_name = 'condition_mobile_schedule' where id = 20;
FINE;
        $this->execute($sql);
    }

    public function down()
    {
        $this->dropColumn('{{%condition_type}}', 'is_for_display');
        $this->dropColumn('{{%condition_type}}', 'is_for_video');
        $this->dropColumn('{{%condition_type}}', 'is_for_mobile');
        $this->dropColumn('{{%condition_type}}', 'table_name');
        return $this->execute('delete from {{%condition_type}} where id between 14 and 20');
    }

    /*
    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}
